//
//  NWStatusViewModel.swift
//  Weibo
//
//  Created by Northwan on 16/1/24.
//  Copyright © 2016年 Northwan. All rights reserved.
//

import UIKit

class NWStatusListViewModel: NSObject {
    
    var statusList:[NWStatusViewModel]?
    
    
    func loadDdata(isPullUp: Bool,finished:(isSucessed:Bool, count: Int)->()){
        
        //上拉
        var maxid = isPullUp ? (statusList?.last?.status?.id ?? 0) : 0
        if maxid > 0{
            maxid -= 1
        }
        
        //下拉
        let sinceid = isPullUp ? 0 : (statusList?.first?.status?.id ?? 0)
        
        NWNetworkTools.shareTools.loadStatus(NWUserAccountViewModel.sharedAccount.accessToken!,maxid:maxid,sinceid: sinceid) { (response, error) -> Void in
            if error != nil {
                finished(isSucessed: false,count: 0)
                return
            }
            
            guard let statusesDict =  response?["statuses"] as? [[String:AnyObject]] else{
                return
            }
            
            var tempArray = [NWStatusViewModel]()
            
            //遍历数组转成模型
            for statusDict in statusesDict {
                let status = NWStatus(dict: statusDict)
                tempArray.append(NWStatusViewModel(status:status))
            }
            
            if self.statusList == nil {
                self.statusList = [NWStatusViewModel]()
            }
            
            if isPullUp {
                self.statusList = self.statusList! + tempArray
            }else{
                self.statusList = tempArray + self.statusList!
            }
            
            finished(isSucessed: true,count:tempArray.count)
        }
    }
}
